#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/param.h>
#include <linux/jiffies.h>
#include <asm/system.h>
#include <asm/processor.h>
#include <asm/signal.h>

static pid_t thread_id;
static DECLARE_COMPLETION(exit_completion);
static atomic_t time_to_quit = ATOMIC_INIT(0);
 
int my_fuction(void *arg) 
{     
	int ret;

	daemonize("demo-thread");        
	allow_signal(SIGKILL);
	complete (&exit_completion);

	while(!signal_pending(current))        
	{                
		printk("jiffies is %lu/n", jiffies);

		set_current_stat(TASK_INTERRUPTIBLE);                                   
		schedule_timeout(10 * HZ);

		if (atomic_read(&kthread->terminate))
		{ /* we received a request to terminate ourself */ break; }
	}

	/*
	for(;;)
	{
		Ret = wait_event_interruptible(wq, condition); //可采用任何同步措施
	}*/

	complete_and_exit(&exit_completion, 1); 

	return 0;
}

static int __init init(void)
{        
	thread_id = kernel_thread(my_fuction, NULL, CLONE_FS | CLONE_FILES);
	wait_for_completion(&exit_completion);

	return 0;
}

static void __exit finish(void)
{     
	atomic_inc(&time_to_quit); 
	kill_proc(thread_id, SIGKILL, 1); 
	wait_for_completion(&exit_completion);
	printk("Goodbye/n");
}

module_init(init);
module_exit(finish);
MODULE_LICENSE("GPL");